package com.anzym.android.kegdroiddisplay;



import android.app.Fragment;
import android.content.Context;
import android.graphics.Canvas;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;

public class KegDroidMapFragment extends Fragment {
	
	private String TAG = "KegDroidMapFragement";
	
	static final int NUMDROIDS = 3;
	
	ImageView kegDroid[] = new ImageView[NUMDROIDS];
	int kegDroidViews[] = new int[NUMDROIDS];
	double kegDroidLeft[] = new double[NUMDROIDS];
	double kegDroidTop[] = new double[NUMDROIDS];
	
	View view;
	int height = 0;
	int width = 0;
	int iconSize = 0;
	
	FrameLayout.LayoutParams params[] = new FrameLayout.LayoutParams[4];
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		
		view = inflater.inflate(R.layout.kegdroidmap, container, false);
		
		FrameLayout fL1 = (FrameLayout) view.findViewById(R.id.map_fragment);
        // then this activity adds in a new MyCustomView object into the fL1
        fL1.addView(new MyView(getActivity().getApplicationContext(), this));
        
        kegDroidViews[0] = R.id.kegdroid_1;
        kegDroidViews[1] = R.id.kegdroid_2;
        kegDroidViews[2] = R.id.kegdroid_3;
        //kegDroidViews[3] = R.id.kegdroid_4;
        
        kegDroidLeft[0] = .27;
        kegDroidLeft[1] = .44;
        kegDroidLeft[2] = .27;
        //kegDroidLeft[3] = .44;
        
        kegDroidTop[0] = .09;
        kegDroidTop[1] = .09;
        kegDroidTop[2] = .63;
        //kegDroidTop[3] = .63;

		return view;
	}

	
	public void setActiveDroid(final int kd) {
		getActivity().runOnUiThread(new Runnable() {
			public void run() {
				try {
					resetDroids();
					kegDroid[kd-1].setImageResource(R.drawable.kdicon_selected);
					view.postInvalidate();
				}catch (Exception e) {
					Log.e(TAG, "Caught Exception " + e);
				}
			}
		});
				
	}
	
	public void resetDroids() {
		//Log.d(TAG, "Reseting Droids");
		//Log.d(TAG, "kegDroid.lenth= " + kegDroid.length);
		
		getActivity().runOnUiThread(new Runnable() {
			public void run() {
				try {
					for (int i = 0; i < kegDroid.length; i++) {
						kegDroid[i].setImageResource(R.drawable.kdicon);
					}
					view.postInvalidate();
				}catch (Exception e) {
					Log.e(TAG, "Caught Exception " + e);
				}
			}
		});
		
	}


	public void setDroids(int viewHeight, int viewWidth) {
		height = viewHeight;
		width = viewWidth;
		iconSize = (int)(width*.05);
		//Log.d(TAG,"Height= " + height + " Width= " + width);
		
		for (int i = 0; i < kegDroid.length; i++) {
	    	kegDroid[i] = (ImageView) view.findViewById(kegDroidViews[i]);
			kegDroid[i].setImageResource(R.drawable.kdicon);
	    	params[i] = new FrameLayout.LayoutParams(iconSize, iconSize);
	    	params[i].leftMargin = (int)(width*kegDroidLeft[i]);
	    	params[i].topMargin = (int)(height*kegDroidTop[i]);
	       	kegDroid[i].setLayoutParams(params[i]);
		}
		
		
	}
	
}
